package com.homework.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hwpf.extractor.WordExtractor;

import com.homework.bean.CharsetBean;
import com.homework.bean.CourseBean;
import com.homework.bean.HomeworkBean;
import com.homework.bean.HomeworkListBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.SimilarityBean;
import com.homework.bean.StudentHomeworkBean;
import com.homework.bean.StudentHomeworkListBean;
import com.homework.bean.UserBean;
import com.homework.bean.UserSimilarityBean;
import com.homework.service.PageInfoService;
import com.homework.util.PublicMethod;
import com.homework.util.SaeStorageIO;
import com.homework.util.SimilarityCheck;
import com.homework.util.TxtToMyHomework;
import com.homework.util.connDB;
import com.sina.sae.storage.SaeStorage;

public class HomeworkDao {
	private static HomeworkDao homeworkDao = null;
	private connDB conn = new connDB();
	private ResultSet rs;
	private PageInfoService pageInfoService;
	// sql语句
	private String sql = "";

	private static String pathEva =  PublicMethod.getEvaPath();
	private static String pathADDR = PublicMethod.getHomeworkPath();
	private static String pathVIC = PublicMethod.getVicPath();
	private String dir = "";
	
	public HomeworkDao() {
		pageInfoService = new PageInfoService();
	}

	public static HomeworkDao getInstance() {
		if (homeworkDao == null) {
			homeworkDao = new HomeworkDao();
		}
		return homeworkDao;
	}

	// 课程作业_教师
	public StudentHomeworkListBean homeworkShow_teacher(CourseBean courseBean,
			PageInfoBean pageInfoBean) throws SQLException {

		// 数据库连接
		sql = "select t_userTemp2.*,t_homeworkTemp.* from (select t_userTemp.* from (select t_chooseTemp.f_chooseId,t_user.* from t_user natural join(select f_chooseId,f_userId from t_choose where f_chooseStatus = 1 and f_courseId = "
				+ courseBean.getCourseId()
				+ ") as t_chooseTemp) as t_userTemp where t_userTemp.f_roleId <> 2 and t_userTemp.f_userStatus <> 5) as t_userTemp2 natural join (select * from t_homework where f_homeworkStatus <> 5) as t_homeworkTemp;";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			StudentHomeworkBean studentHomeworkBean = new StudentHomeworkBean();
			HomeworkBean homeworkBean = new HomeworkBean();
			UserBean userBean = new UserBean();
			homeworkBean.setHomeworkId(rs.getString("f_homeworkId"));
			homeworkBean.setHomeworkName(rs.getString("f_homeworkName"));
			homeworkBean.setBatch(rs.getString("f_batch"));
			homeworkBean.setScore(rs.getString("f_score"));
			homeworkBean.setAddress(rs.getString("f_address"));
			homeworkBean.setTrack(rs.getString("f_track"));
			homeworkBean.setDescribe(rs.getString("f_describe"));
			homeworkBean.setHomeworkStatus(rs.getString("f_homeworkStatus"));
			homeworkBean.setChooseId(rs.getString("f_chooseId"));
			userBean.setUserId(rs.getString("f_userId"));
			userBean.setUsername(rs.getString("f_username"));
			userBean.setRealname(rs.getString("f_realname"));
			userBean.setUserStatus(rs.getString("f_userStatus"));
			userBean.setPhone(rs.getString("f_phone"));
			userBean.setEmail(rs.getString("f_email"));
			userBean.setLasttime(rs.getString("f_lasttime"));
			userBean.setLoginip(rs.getString("f_loginip"));
			userBean.setCreatonTime(rs.getString("f_creationTime"));
			userBean.setRoleId(rs.getString("f_roleId"));
			userBean.setUniversity(rs.getString("f_university"));
			userBean.setInstitute(rs.getString("f_institute"));
			userBean.setGrade(rs.getString("f_grade"));
			userBean.setMajor(rs.getString("f_major"));
			userBean.setClasses(rs.getString("f_classes"));
			studentHomeworkBean.setHomeworkBean(homeworkBean);
			studentHomeworkBean.setUserBean(userBean);
			studentHomeworkListBean.getStudentHomeworkBeanList().add(
					studentHomeworkBean);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return studentHomeworkListBean;
	}

	// 作业查询_教师(根据realname和courseId查找)
	public StudentHomeworkListBean homeworkQuery_teacher(CourseBean courseBean,
			PageInfoBean pageInfoBean, UserBean userBean) throws SQLException {
		// 数据库连接
		sql = "select t_userTemp2.*,t_homeworkTemp.* from ( select t_chooseTemp.f_chooseId,t_userTemp.* from (select * from t_user where f_realname like '%"
				+ userBean.getRealname()
				+ "%' and f_roleId <> 2 and f_userStatus <> 5)as t_userTemp natural join(select f_chooseId,f_userId from t_choose where f_chooseStatus = 1 and f_courseId = "
				+ courseBean.getCourseId()
				+ ") as t_chooseTemp) as t_userTemp2 natural join (select * from t_homework where f_homeworkStatus <> 5) as t_homeworkTemp;";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			StudentHomeworkBean studentHomeworkBean = new StudentHomeworkBean();
			HomeworkBean homeworkBean = new HomeworkBean();
			UserBean userBeanTemp = new UserBean();
			homeworkBean.setHomeworkId(rs.getString("f_homeworkId"));
			homeworkBean.setHomeworkName(rs.getString("f_homeworkName"));
			homeworkBean.setBatch(rs.getString("f_batch"));
			homeworkBean.setScore(rs.getString("f_score"));
			homeworkBean.setAddress(rs.getString("f_address"));
			homeworkBean.setTrack(rs.getString("f_track"));
			homeworkBean.setDescribe(rs.getString("f_describe"));
			homeworkBean.setHomeworkStatus(rs.getString("f_homeworkStatus"));
			homeworkBean.setChooseId(rs.getString("f_chooseId"));
			userBeanTemp.setUserId(rs.getString("f_userId"));
			userBeanTemp.setUsername(rs.getString("f_username"));
			userBeanTemp.setRealname(rs.getString("f_realname"));
			userBeanTemp.setUserStatus(rs.getString("f_userStatus"));
			userBeanTemp.setPhone(rs.getString("f_phone"));
			userBeanTemp.setEmail(rs.getString("f_email"));
			userBeanTemp.setLasttime(rs.getString("f_lasttime"));
			userBeanTemp.setLoginip(rs.getString("f_loginip"));
			userBeanTemp.setCreatonTime(rs.getString("f_creationTime"));
			userBeanTemp.setRoleId(rs.getString("f_roleId"));
			userBeanTemp.setUniversity(rs.getString("f_university"));
			userBeanTemp.setInstitute(rs.getString("f_institute"));
			userBeanTemp.setGrade(rs.getString("f_grade"));
			userBeanTemp.setMajor(rs.getString("f_major"));
			userBeanTemp.setClasses(rs.getString("f_classes"));
			studentHomeworkBean.setHomeworkBean(homeworkBean);
			studentHomeworkBean.setUserBean(userBeanTemp);
			studentHomeworkListBean.getStudentHomeworkBeanList().add(
					studentHomeworkBean);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return studentHomeworkListBean;
	}

	// 课程作业_学生
	public StudentHomeworkListBean homeworkShow_student(CourseBean courseBean,
			PageInfoBean pageInfoBean, UserBean userBean) throws SQLException {

		// 数据库连接
		sql = "select * from t_homework where f_homeworkStatus <> 5 and f_chooseId =(select f_chooseId from t_choose where f_chooseStatus <> 5 and f_courseId = "
				+ courseBean.getCourseId()
				+ " and f_userId = "
				+ userBean.getUserId() + ");";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			StudentHomeworkBean studentHomeworkBean = new StudentHomeworkBean();
			HomeworkBean homeworkBean = new HomeworkBean();
			UserBean userBeanTemp = new UserBean();
			homeworkBean.setHomeworkId(rs.getString("f_homeworkId"));
			homeworkBean.setHomeworkName(rs.getString("f_homeworkName"));
			homeworkBean.setBatch(rs.getString("f_batch"));
			homeworkBean.setScore(rs.getString("f_score"));
			homeworkBean.setAddress(rs.getString("f_address"));
			homeworkBean.setTrack(rs.getString("f_track"));
			homeworkBean.setDescribe(rs.getString("f_describe"));
			homeworkBean.setHomeworkStatus(rs.getString("f_homeworkStatus"));
			homeworkBean.setChooseId(rs.getString("f_chooseId"));
			studentHomeworkBean.setHomeworkBean(homeworkBean);
			studentHomeworkBean.setUserBean(userBeanTemp);
			studentHomeworkListBean.getStudentHomeworkBeanList().add(
					studentHomeworkBean);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return studentHomeworkListBean;
	}

	// 作业查询_学生(根据realname和courseId查找)
	public StudentHomeworkListBean homeworkQuery_student(CourseBean courseBean,
			PageInfoBean pageInfoBean, UserBean userBean,
			HomeworkBean homeworkBean) throws SQLException {
		// 数据库连接
		if (homeworkBean.getBatch().equals("")) {
			sql = "select * from t_homework where f_homeworkStatus <> 5 and f_chooseId =(select f_chooseId from t_choose where f_chooseStatus <> 5 and f_courseId = "
					+ courseBean.getCourseId()
					+ " and f_userId = "
					+ userBean.getUserId() + ");";
		} else {
			sql = "select * from t_homework where f_homeworkStatus <> 5 and f_chooseId =(select f_chooseId from t_choose where f_chooseStatus <> 5 and f_courseId = "
					+ courseBean.getCourseId()
					+ " and f_userId = "
					+ userBean.getUserId()
					+ ") and f_batch = "
					+ homeworkBean.getBatch() + ";";
		}

		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			StudentHomeworkBean studentHomeworkBeanTemp = new StudentHomeworkBean();
			HomeworkBean homeworkBeanTemp = new HomeworkBean();
			UserBean userBeanTemp = new UserBean();
			homeworkBeanTemp.setHomeworkId(rs.getString("f_homeworkId"));
			homeworkBeanTemp.setHomeworkName(rs.getString("f_homeworkName"));
			homeworkBeanTemp.setBatch(rs.getString("f_batch"));
			homeworkBeanTemp.setScore(rs.getString("f_score"));
			homeworkBeanTemp.setAddress(rs.getString("f_address"));
			homeworkBeanTemp.setTrack(rs.getString("f_track"));
			homeworkBeanTemp.setDescribe(rs.getString("f_describe"));
			homeworkBeanTemp
					.setHomeworkStatus(rs.getString("f_homeworkStatus"));
			homeworkBeanTemp.setChooseId(rs.getString("f_chooseId"));
			studentHomeworkBeanTemp.setHomeworkBean(homeworkBeanTemp);
			studentHomeworkBeanTemp.setUserBean(userBeanTemp);
			studentHomeworkListBean.getStudentHomeworkBeanList().add(
					studentHomeworkBeanTemp);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return studentHomeworkListBean;
	}

	public String setDir(int chooseId){	
		try {
			int num = 0;
			String sql = "select f_homeworkNum from t_choose where f_chooseId=" + chooseId;
			rs = conn.executeQuery(sql);
			if (rs.next()) {
				num = (rs.getInt("f_homeworkNum"));
				rs.close();
				conn.close();
			}
			num++;
			String sql1 = "update t_choose set f_homeworkNum=" + num + " where f_chooseId=" + chooseId;
			conn.executeUpdate(sql1);
			
			
			dir = String.format("%05d", chooseId) + String.format("%05d", num) + ".txt";
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return dir;
	}
	
	
	public int getChooseId(UserBean userBean,CourseBean courseBean){	
		sql = "select f_chooseId from t_choose where f_userId = " + userBean.getUserId() + " and f_courseId = "+ courseBean.getCourseId();
		rs = conn.executeQuery(sql);
		int chooseId = 0;
		try {
			if(rs.next()){
				chooseId = rs.getInt("f_chooseId");
			}
			rs.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return chooseId;
			
	}
	
	// 作业列表_老师布置
	public HomeworkListBean homeworkShow_arrange(CourseBean courseBean,
			PageInfoBean pageInfoBean) throws SQLException {
		// 数据库连接
		sql = "select * from (select t_chooseTemp.f_chooseId from (select f_userId,f_chooseId from t_choose where f_courseId = "
				+ courseBean.getCourseId()
				+ " and f_chooseStatus = 1) as t_chooseTemp natural join (select f_userId from t_user where f_roleId = 2 and f_userStatus <>5) as t_userTemp ) as t_temp natural join t_homework;";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取HomeworkListBean
		HomeworkListBean homeworkListBean = new HomeworkListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			HomeworkBean homeworkBeanTemp = new HomeworkBean();
			homeworkBeanTemp.setHomeworkId(rs.getString("f_homeworkId"));
			homeworkBeanTemp.setHomeworkName(rs.getString("f_homeworkName"));
			homeworkBeanTemp.setBatch(rs.getString("f_batch"));
			homeworkBeanTemp.setScore(rs.getString("f_score"));
			homeworkBeanTemp.setAddress(rs.getString("f_address"));
			homeworkBeanTemp.setTrack(rs.getString("f_track"));
			homeworkBeanTemp.setDescribe(rs.getString("f_describe"));
			homeworkBeanTemp.setHomeworkStatus(rs.getString("f_homeworkStatus"));
			homeworkBeanTemp.setChooseId(rs.getString("f_chooseId"));
			homeworkListBean.getHomeworkList().add(homeworkBeanTemp);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return homeworkListBean;
	}

	// 作业上传
	public void homeworkUpload(UserBean userBean, CourseBean courseBean,
			HomeworkBean homeworkBean) throws SQLException {
		
		// 数据库连接
		sql = "insert into t_homework (f_homeworkName,f_batch,f_address,f_track,f_homeworkVirt,f_describe,f_homeworkStatus,f_chooseId) values ('"
				+ homeworkBean.getHomeworkName()
				+ "',"
				+ homeworkBean.getBatch()
				+ ",'"
				+ homeworkBean.getAddress()
				+ "','"
				+ homeworkBean.getTrack()
				+ "','"
				+ homeworkBean.getHomeworkVic()
				+ "','"
				+ homeworkBean.getDescribe()
				+ "',1,(select f_chooseId from t_choose where f_userId = "
				+ userBean.getUserId()
				+ " and f_courseId = "
				+ courseBean.getCourseId() + "));";
		conn.executeUpdate(sql);
		conn.close();
		sql = "select max(f_homeworkId) from t_homework";
		rs = conn.executeQuery(sql);
		if (rs.next()) {
			homeworkBean.setHomeworkId(rs.getString("max(f_homeworkId)"));
		}
		rs.close();
		conn.close();
	}

	public String setDirTrack(int chooseId) {
		// TODO Auto-generated method stub
		return (pathEva+setDir(chooseId));
	}
	
	public String setDirAddress(int chooseId) {
		// TODO Auto-generated method stub
		return (pathADDR+dir);
	}
	public String setDirVic(int chooseId) {
		// TODO Auto-generated method stub
		return (pathVIC+dir);
	}
	
	

	// 获取作业批次
	public int getBatch(UserBean userBean, CourseBean courseBean)
			throws SQLException {
		sql = "select  f_batch from t_homework where f_chooseId = (select f_chooseId from t_choose where f_courseId = "
				+ courseBean.getCourseId()
				+ " and f_userId = "
				+ userBean.getUserId() + ") ;";
		rs = conn.executeQuery(sql);
		rs.last();
		int max = rs.getRow();
		rs.close();
		conn.close();
		return max;
	}

	// 布置作业
	public void homeworkRequirement(UserBean userBean, CourseBean courseBean,
			HomeworkBean homeworkBean) {
		// 数据库连接
		sql = "insert into t_homework (f_homeworkName,f_batch,f_describe,f_homeworkStatus,f_chooseId) values ('"
				+ homeworkBean.getHomeworkName()
				+ "',"
				+ homeworkBean.getBatch()
				+ ",'"
				+ homeworkBean.getDescribe()
				+ "',1,(select f_chooseId from t_choose where f_userId = "
				+ userBean.getUserId()
				+ " and f_courseId = "
				+ courseBean.getCourseId() + "));";
		conn.executeUpdate(sql);
		conn.close();
	}
	
	public String getBatchOfHomeworkT(String homeworkId){
		sql = "select f_batch from t_homework where f_homeworkId = " + homeworkId;
		rs = conn.executeQuery(sql);
		try {
			if (rs != null)
				return rs.getString("f_describe");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	// 下载作业
	public String homeworkDownload(HomeworkBean homeworkBean)
			throws SQLException {
		sql = "select f_address from t_homework where f_homeworkId = "
				+ homeworkBean.getHomeworkId() + ";";
		rs = conn.executeQuery(sql);
		String address = "";
		if (rs.next()) {
			address = rs.getString("f_address");
		}
		rs.close();
		conn.close();
		return address;
	}

	// 修改成绩
	public void modifyScore(HomeworkBean homeworkBean) {
		// 数据库连接
		sql = "update t_homework set f_score = " + homeworkBean.getScore()
				+ " where f_homeworkId = " + homeworkBean.getHomeworkId() + ";";
		conn.executeUpdate(sql);
		conn.close();
	}

	// 通过homeworkId获取realname
	public UserBean getRealName(int homeworkId) throws SQLException {
		sql = "select f_realname from t_user where f_userStatus <> 5 and f_userId = (select f_userId from t_choose where f_chooseStatus = 1 and f_chooseId = (select f_chooseId from t_homework where f_homeworkId = "
				+ homeworkId + " and f_homeworkStatus <> 5));";
		rs = conn.executeQuery(sql);
		UserBean userBean = new UserBean();
		if (rs.next()) {
			userBean.setRealname(rs.getString("f_realname"));
		}
		return userBean;

	}

	// 检查该作业与所有已上传作业的相似度
	public boolean similarityCheck(HomeworkBean homeworkBean,
			CourseBean courseBean) {

		SimilarityBean similarityBean = new SimilarityBean();
		SimilarityCheck sc = new SimilarityCheck();

		sql = "select distinct t_homeworkTemp.* from "
				+ "(select t_homework.f_homeworkId,t_homework.f_address"
				+ " from t_homework where f_batch = "
				+ homeworkBean.getBatch()
				+ " and f_homeworkStatus = 1) "
				+ "as t_homeworkTemp natural join (select f_chooseId  from t_choose"
				+ " where f_courseId = " + courseBean.getCourseId()
				+ " and f_chooseStatus = 1) as t_chooseTemp";
		rs = conn.executeQuery(sql);
		try {
			while (rs.next()) {
				similarityBean.setHomeworkId(homeworkBean.getHomeworkId());
				similarityBean.setHomeworkIdB(rs.getString("f_homeworkId"));
				similarityBean.setSimilarity(String.valueOf(sc.Compare(
						homeworkBean.getAddress(), rs.getString("f_address"))));
				saveResult(similarityBean);// 保存
			}
			rs.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	// 保存比较结果到数据库
	public boolean saveResult(SimilarityBean similarityBean) {
		sql = "insert into t_similarity values(null,"
				+ similarityBean.getSimilarity() + ","
				+ similarityBean.getHomeworkId() + ","
				+ similarityBean.getHomeworkIdB() + ",1)";
		int result = conn.executeUpdate(sql);
		if (result == 1) {
			return true;
		}
		return false;
	}

	public boolean getHomeworkInfo(List<UserSimilarityBean> userSimilarity,
			String courseId, HomeworkBean homeworkBean) {
		sql = "select distinct f_similarityId from t_similarity,(select t_homeworkTemp.f_homeworkId from (select t_chooseTemp.f_chooseId from (select f_userId,f_realname from t_user where f_roleId = 1 and f_userStatus <> 5) as t_userTemp natural join (select f_userId,f_chooseId from t_choose where f_courseId = "
				+ courseId
				+ " and f_chooseStatus = 1) as t_chooseTemp ) as t_temp natural join (select t_homework.f_homeworkId,t_homework.f_chooseId from t_homework where f_batch = "
				+ homeworkBean.getBatch()
				+ " and f_homeworkStatus <>5) as t_homeworkTemp ) as t_temp2 where t_similarity.f_homeworkId = t_temp2.f_homeworkId or t_similarity.f_homeworkIdB = t_temp2.f_homeworkId ;";
		rs = conn.executeQuery(sql);
		try {
			List<String> similarityIdList = new ArrayList<String>();
			while (rs.next()) {
				similarityIdList.add(rs.getString("f_similarityId"));
			}
			for (int i = 0; i < similarityIdList.size(); i++) {
				selectByHomeworkId(userSimilarity, similarityIdList.get(i));
			}

			rs.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	// 通过HomeworkId找到HomeworkIdB及f_similarity
	public boolean selectByHomeworkId(List<UserSimilarityBean> userSimilarity,
			String similarityId) {

		sql = "select f_similarity,f_homeworkId,f_homeworkIdB from t_similarity where f_similarityId = "
				+ similarityId + "";
		rs = conn.executeQuery(sql);
		try {
			while (rs.next()) {
				UserSimilarityBean userSimilarityBean = new UserSimilarityBean();

				SimilarityBean similarityBean = new SimilarityBean();
				UserBean userA = null;
				UserBean userB = null;

				similarityBean.setHomeworkId(rs.getString("f_homeworkId"));
				similarityBean.setHomeworkIdB(rs.getString("f_homeworkIdB"));
				similarityBean.setSimilarity(rs.getString("f_similarity"));
				userB = getRealName(Integer.parseInt(similarityBean
						.getHomeworkIdB()));
				userA = getRealName(Integer.parseInt(similarityBean
						.getHomeworkId()));

				userSimilarityBean.setSimilarityBean(similarityBean);
				userSimilarityBean.setUserA(userA);
				userSimilarityBean.setUserB(userB);

				userSimilarity.add(userSimilarityBean);
			}
			rs.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// 动态获取batch
	public int getBatchNum(String courseId) throws NumberFormatException, SQLException {
		sql = "select max(f_batch) from (select * from t_choose where f_courseId = "
				+ courseId
				+ " and f_chooseStatus = 1 ) as t_chooseTemp natural join t_homework;";
		rs = conn.executeQuery(sql);
		int num = 0;
		if(rs.next()){
			num = Integer.parseInt(rs.getString(1));
		}
		return num;
	}

	public boolean saveVirtAndEva(String lastString, HomeworkBean homeworkBean) throws IOException {
		// TODO Auto-generated method stub
		CharsetBean charsetBean = new CharsetBean();
		String address = homeworkBean.getAddress();
		String realh = getRealHomework(address,lastString,charsetBean);
		String json = TxtToMyHomework.txtToMyHomework(realh);
		saveHomeworkVirt(json, homeworkBean.getHomeworkVic());
		saveEva(homeworkBean.getTrack());
	
		return true;
	}

	public boolean saveEva(String track){
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		ss.write("homework",track,"");
		return true;
	}
	
	public boolean saveHomeworkVirt(String json,String homeworkVirt)
	{
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		ss.write("homework",homeworkVirt,json);	
		return true;
	}
	
	private String getRealHomework(String address,String lastString,CharsetBean charsetBean) throws IOException {
		InputStream is = null;
		if(lastString.equals(".doc"))
		{
			WordExtractor wordExtractor;
			try {
				is = (SaeStorageIO.getInputStreamOnStorage(address,charsetBean));
				wordExtractor = new WordExtractor(is);
				is.close();
				return wordExtractor.getText();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return "";
		}
		else if(lastString.equals(".txt"))
		{
			String realh = "";
			try {
				is = SaeStorageIO.getInputStreamOnStorage(address,charsetBean);
				byte[] temp = new byte[charsetBean.getCharLength()];
				is.read(temp,0,charsetBean.getCharLength());
				realh = new String(temp,charsetBean.getCharsetName());			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				is.close();
			}
			return realh;
		}
		return "";
	}
}
